/*
    This file is part of MOOPerformanceAssessment.

    MOOPerformanceAssessment is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    MOOPerformanceAssessment is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MOOPerformanceAssessment; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Authors: Thomas Voß, 2007
    Contact: thomas.voss.bochum@googlemail.com
*/

#include <PisaFileOutputPostProcessor.h>

#include <MOOPerformanceAssessmentConfig.h>
#include <MOOPerformanceAssessmentGlobal.h>

#include <QFile>
#include <QTextStream>

QTextStream & operator<<( QTextStream & stream, const Point & point )
{
    for( unsigned i = 0; i < point.size(); i++ )
	stream << point[i] << " ";

    return( stream );
}

QTextStream & operator<<( QTextStream & stream, const PointSet & pointSet )
{
    for( unsigned i = 0; i < pointSet.size(); i++ )
	stream << pointSet[i] << endl;

    return( stream );
}

void PisaFileOutputPostProcessor::operator()( const std::vector<PointSet> & referenceDataSets, 
					      const std::vector<std::vector<std::vector<PointSet> > > & algorithmData, 
					      const std::vector<std::vector<std::vector<std::vector<double> > > > & indicatorValues,
					      const std::vector<std::vector<std::vector<std::vector<double> > > > & significances,
					      const MOOPerformanceAssessmentConfig & config )
{
    // Process reference data sets.
    for( int i = 0; i < config.m_functions.size(); i++ )
	{
	    QString uri( "%1/%2_ref.%3" );
	    uri = uri.arg(config.m_resultDir).arg( config.m_functions.at( i ) ).arg( config.m_generations );
	    QFile file( uri ); file.open( QIODevice::ReadWrite | QIODevice::Text );

	    QTextStream stream( &file );
	    stream << referenceDataSets[i];
	}
    
    // Process normalized algorithm data
    for( int i = 0; i < config.m_functions.size(); i++ ) 	// functions
	for( int j = 0; j < config.m_algorithms.size(); j++ ) 	// algortihms
	    {
		QString uri( "%1/%2_%3_norm.%4" );
		uri = uri.arg(config.m_resultDir).arg( config.m_functions.at( i ) ).arg( config.m_algorithms.at( j ) ).arg( config.m_generations );
		QFile file( uri ); file.open( QIODevice::ReadWrite | QIODevice::Text );
		
		QTextStream stream( &file );
		for( int k = 0; k < config.m_trials; k++ )  	// trials
		    {
			
			stream << algorithmData[i][j][k];
		    }	
	    }

    // Process indicator values
    for( int i = 0; i < indicatorValues.size(); i++ ) // functions
	for( int j = 0; j < indicatorValues[i].size(); j++ ) // algorithms
	    for( int k = 0; k < config.m_vIndicators.size(); k++ ) // indicators
		{
		    QString uri( "%1/%2_%3_%4.%5" );
		    uri = uri.arg(config.m_resultDir).arg( config.m_functions.at( i ) ).arg( config.m_algorithms.at( j ) ).arg( k ).arg( config.m_generations );
		    QFile file( uri ); file.open( QIODevice::ReadWrite | QIODevice::Text );
		
		    QTextStream stream( &file );

		    for( int l = 0; l < config.m_trials; l++ ) // trials
			{
			    stream << indicatorValues[i][j][l][k] << endl;
			}		    
		}
}

extern "C" IPostProcessor * factory() { return( new PisaFileOutputPostProcessor() ); } 

